const path = require('path');
const webpack = require('webpack');
const htmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');

module.exports = {
    devtool: 'inline-source-map',
    entry: {
        app: './app/app.main.js',
        vendor: ['angular', 'angular-animate', 'angular-toastr', 'angular-ui-bootstrap', 'angular-ui-grid', 'angular-ui-router', 'bootstrap', 'echarts', 'jquery', 'oi.select']
    },
    output: {
        path: path.join(__dirname, 'build'),
        filename: 'js/[name].[hash:8].js',
        chunkFilename: "chunks/[name].chunk.[hash:8].js"
    },
    resolve: {
        extensions: ['.js', '.ts', '.json', '.html', '.less', '.css'],
        modules: [path.join(__dirname, "app"), "node_modules"],
        alias: {
            jquery: "jquery/dist/jquery.min" /* 引入jquery */
        }
    },
    module: {
        rules: [{
            test: /\.html$/,
            use: ['raw-loader'],
            exclude: /node_modules/
        }, {
            test: /\.js(x)*$/,
            use: ['babel-loader'],
            exclude: /node_modules/
        }, {
            test: /\.less$/,
            use: ['style-loader', 'css-loader', 'less-loader', 'postcss-loader'],
            include: path.join(__dirname, 'app')
        }, {
            test: /\.css$/,
            use: ['style-loader', 'css-loader', 'postcss-loader']
        }, {
            test: /\.(jpg|png|gif|ico)$/,
            use: ['file-loader?name=./img/[name].[ext]']
        }, {
            test: /\.(woff|woff2|eot|ttf|svg)$/,
            use: ['url-loader?limit:10000&name=./fonts/[name].[ext]']
        }]
    },
    plugins: [
        new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.HashedModuleIdsPlugin(),
        new webpack.LoaderOptionsPlugin({
            options: {
                postcss: function() { return [autoprefixer({ browsers: ['last 2 versions'] })] }
            },
            devServer: {
                port: 8080,
                historyApiFallback: true
            }
        }),
        new webpack.optimize.CommonsChunkPlugin({
            names: ['vendor', 'manifest'],
            minChunks: function(module) {
                return module.context && module.context.indexOf('node_modules') !== -1;
            }
        }),
        new htmlWebpackPlugin({
            template: path.resolve(__dirname, 'app/index.html'),
            favicon: path.resolve(__dirname, 'app/img/favicon.ico')
        }),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery'
        })
    ]
};
